package images;
/***********************
 * This is a class designed to read and store keys input
 * by the player. The keys are then transferred to the control class
 * to incur consequences.
 */
import java.awt.event.*;
public class KeyInput extends KeyAdapter {
        //This one byte holds all the keys that have been pressed. yep.
        //Since a byte is 8 bits, that means there is a maximum of 4 arrow keys and 4 other buttons.
        //To expand that, in the case of multiplayer, for example, every byte would need to be a short.
        private byte queue;
        //The panel from which to get keys
        private static GameControl window;

        public KeyInput(GameControl gc) {
                //Store the jpanel in a field with larger scope
                window = gc;
                //Tell it that this class will be listening for keys
                window.addKeyListener(this);
                queue = 0;
        }

        public void keyPressed(KeyEvent e) {
                int keycode = e.getKeyCode();
                //If the up key is pressed...
                if (keycode == Keys.UP.code) {
                        if ((queue ^ Keys.DOWN.key) == 0)
                                //If the up key is pressed when down is already pressed, fuck the down key I'm goin' up bitch
                                queue = Keys.UP.key;
                        else
                                //In all other situations (left/right/up is already pressed), keep the other keys and add up
                                queue |= Keys.UP.key;
                } else if (keycode == Keys.DOWN.code) {
                        if ((queue ^ Keys.UP.key) == 0)
                                //If the up key is pressed when up is already pressed, fuck the up key I'm goin' down bitch
                                queue = Keys.DOWN.key;
                        else
                                //In all other situations (left/right/down is already pressed), keep the other keys and add down
                                queue |= Keys.DOWN.key;
                } else if (keycode == Keys.LEFT.code) {
                        if ((queue ^ Keys.RIGHT.key) == 0)
								//If the left key is pressed when right is already pressed, fuck the right key I'm goin' left bitch
								queue = Keys.LEFT.key;
						else
								//In all other situations (left/right/up is already pressed), keep the other keys and add up
								queue |= Keys.LEFT.key;
                } else if (keycode == Keys.RIGHT.code) {
                        if ((queue ^ Keys.LEFT.key) == 0)
								//If the left key is pressed when right is already pressed, fuck the right key I'm goin' left bitch
								queue = Keys.RIGHT.key;
						else
								//In all other situations (left/right/up is already pressed), keep the other keys and add up
								queue |= Keys.RIGHT.key;
                } else if (keycode == Keys.BUTTON1.code) {
						queue |= Keys.BUTTON1.key;
				} else if (keycode == Keys.BUTTON2.code) {
						queue |= Keys.BUTTON2.key;
				} else if (keycode == Keys.BUTTON3.code) {
						queue |= Keys.BUTTON3.key;
				}


                window.update(getKey());

        }
        public void keyReleased(KeyEvent e) {
                //Create a local variable for the key that was released
                int keycode = e.getKeyCode();
                //If it's up, remove up from the queue, down = remove down, etc.
                if (keycode == Keys.UP.code)
                        queue ^= (queue & Keys.UP.key);
                else if (keycode == Keys.DOWN.code)
                        queue ^= (queue & Keys.DOWN.key);
                else if (keycode == Keys.LEFT.code)
                        queue ^= (queue & Keys.LEFT.key);
                else if (keycode == Keys.RIGHT.code)
                        queue ^= (queue & Keys.RIGHT.key);
                else if (keycode == Keys.BUTTON1.code)
                		queue ^= (queue & Keys.BUTTON1.key);
                else if (keycode == Keys.BUTTON2.code)
                		queue ^= (queue & Keys.BUTTON2.key);
				else if (keycode == Keys.BUTTON3.code)
                		queue ^= (queue & Keys.BUTTON3.key);

                window.update(getKey());

        }
        private Keys[] getKey() {
				//Returns an array-- the first value in the array is the directional key pressed, the second is the button pressed.
				//If more button functionality is needed, you can simply expand the array: 1 button per bucket.
                Keys[] k = new Keys[2];

				//Note the careful ordering of the if-else block here. Diagonal keys must have priority
				//queue & Keys.x.key returns the flags that queue and the key have in common.
				//it's pretty much just saying "if queue CONTAINS key.x.key"
                if ((queue & Keys.D_DL.key) == Keys.D_DL.key)
						k[0] = Keys.D_DL;
				else if ((queue & Keys.D_UL.key) == Keys.D_UL.key)
						k[0] = Keys.D_UL;
				else if ((queue & Keys.D_UR.key) == Keys.D_UR.key)
						k[0] = Keys.D_UR;
				else if ((queue & Keys.D_DR.key) == Keys.D_DR.key)
						k[0] = Keys.D_DR;
				else if ((queue & Keys.UP.key) == Keys.UP.key)
						k[0] = Keys.UP;
				else if ((queue & Keys.RIGHT.key) == Keys.RIGHT.key)
						k[0] = Keys.RIGHT;
				else if ((queue & Keys.DOWN.key) == Keys.DOWN.key)
						k[0] = Keys.DOWN;
				else if ((queue & Keys.LEFT.key) == Keys.LEFT.key)
						k[0] = Keys.LEFT;
				else
                        k[0] = Keys.ERROR;


				//For now, buttons have reverse priority: button 3 > button 2 > button 1
                if ((queue & Keys.BUTTON3.key) == Keys.BUTTON3.key)
                		k[1] = Keys.BUTTON3;
                else if ((queue & Keys.BUTTON2.key) == Keys.BUTTON2.key)
                		k[1] = Keys.BUTTON2;
                else if ((queue & Keys.BUTTON1.key) == Keys.BUTTON1.key)
                		k[1] = Keys.BUTTON1;
                else
                        k[1] = Keys.ERROR;

                return k;
        }

        public void reset() {
			queue = 0;
		}

}